* Figure 3 Mean outcome variables by village-year insurance coverage
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

*Using balanced panel
keep if bal_panel3==1

sort id mkt_year

rename fraction_hy HY
rename fraction_cashcrop cashcrop

* Easier labelling for graphs
lab var cost_k_w1_real "Total agricultural costs (INR)"
lab var profit_k_w1_real "Total agricultural profits (INR)"
lab var kval_w1_real "Total agricultural revenues (INR)"
lab var fin_costs_real "Total insurance costs (INR)"
lab var fin_revenues_real "Total insurance revenues (INR)"
lab var totalcosts_w1_real "Total costs (INR)"
lab var totalrevenues_w1_real "Total revenues (INR)"
lab var totalprofit_w1_real "Total profit (INR)"
lab var HY "Fraction of HYV crops"
lab var cashcrop "Fraction of cash-crops"
lab var plotsize_ha "Area cultivated (in hectares)"


// Gen averages
foreach var of varlist cost_k_w1_real kval_w1_real profit_k_w1_real fin_costs_real fin_revenues_real totalcosts_w1_real totalrevenues_w1_real totalprofit_w1_real HY cashcrop plotsize_ha {
bys mkt_year: egen avg_`var'_b=mean(`var') if bought==1  
bys mkt_year: egen avg_`var'_nbt=mean(`var') if bought == 0 & treat_year == 1 
bys mkt_year: egen avg_`var'_nt=mean(`var') if treat_year == 0 
lab var avg_`var'_b "Treatment Group: Coverage"
lab var avg_`var'_nbt "Treatment Group: No Coverage"
lab var avg_`var'_nt "Control Group"  
}

foreach var in cost_k_w1_real kval_w1_real profit_k_w1_real fin_costs_real fin_revenues_real totalcosts_w1_real totalrevenues_w1_real totalprofit_w1_real HY cashcrop plotsize_ha{ 
local l`var':variable label `var'
twoway connected  avg_`var'_b avg_`var'_nbt avg_`var'_nt mkt_year, title("`l`var''", size(s)) ylabel(,angle (30)) xlabel(2005(2)2013) xtitle("") graphregion(color(white)) plotregion(icolor(white)) msymbol(T D O)
graph save avg_`var', replace
graph export "../output/graphs/f3_`var'.emf", replace
}

grc1leg avg_cost_k_w1_real.gph avg_kval_w1_real.gph avg_profit_k_w1_real.gph avg_fin_costs_real.gph avg_fin_revenues_real.gph avg_totalcosts_w1_real.gph avg_totalrevenues_w1_real.gph avg_totalprofit_w1_real.gph avg_HY.gph avg_cashcrop.gph avg_plotsize_ha.gph, graphregion(color(white) lwidth(large)) plotregion(icolor(white)) 
graph export "../output/graphs/f3.emf", replace


